/**
 * @file Bsp_Led.c
 * @author 徐松亮 (5387603@qq.com)
 * @version 1.0
 * @date 2023-09-28
 *
 * @copyright Copyright (c) {2030}  沈阳迈思医疗科技有限公司
 *
 * @brief 驱动层-->基于GPIO的LED驱动程序
 */

/* 私有头部文件 (Includes)-----------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "Bsp_Led.h"
/* 编译警告条件 (Compile warning condition)------------------------------------*/
/* 私有类型定义 (Private typedef)----------------------------------------------*/
/* 私有型宏定义 (Private define)-----------------------------------------------*/
/* 私有常量定义 (Private constants)--------------------------------------------*/
/* 公有常量定义 (Public constants)---------------------------------------------*/
/* 应用性宏定义 (Application define)-------------------------------------------*/
/* 私有变量定义 (Private variables)--------------------------------------------*/
/* 私有函数声明 (Private function prototypes)----------------------------------*/
static void BspLed_Cycle_1ms(void *_tp_interface);
static void BspLed_Cycle_100ms(void *_tp_interface);
static void BspLed_PwmLed(void *_tp_interface, BSP_LED_E_PWM_LEVEL _e_pwmLevel);
static void BspLed_Mode(void *_tp_interface, BSP_LED_E_MODE mode, BSP_LED_E_PRIORITY _e_Priority);
/* 私有函数实体 (Private functions)--------------------------------------------*/

/**
 * @brief   周期执行函数
 * @note    用于LED的调光
 * @param   None
 * @return  None
 */
static void BspLed_Cycle_1ms(void *_tp_interface)
{
    BSP_LED_T_INTERFACE *tp_interface = _tp_interface;
    // 调试过程不操作
    if (tp_interface->t_Private.uc_DebugTestEnable == 1)
    {
        return;
    }
    //
    if (tp_interface->t_Private.uc_PwmCmt < tp_interface->t_Private.e_PwmLevel)
    {
        tp_interface->t_Input.fnp_LedOnOff(true);
        tp_interface->t_Private.uc_PwmCmt++;
    }
    else if (tp_interface->t_Private.uc_PwmCmt < 10)
    {
        tp_interface->t_Input.fnp_LedOnOff(false);
        tp_interface->t_Private.uc_PwmCmt++;
    }
    else
    {
        tp_interface->t_Private.uc_PwmCmt = 0;
    }
}

/**
 * @brief   周期执行函数
 * @note    用于LED的Debug测试,开启Debug相关指令后会循环点亮
 * @param   None
 * @return  None
 */
static void BspLed_Cycle_100ms(void *_tp_interface)
{
    uint8_t i = 0;
    BSP_LED_T_INTERFACE *tp_interface = _tp_interface;
    // 非调试状态
	tp_interface->t_Private.uc_DebugTestEnable = 1;//add     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	
    if (tp_interface->t_Private.uc_DebugTestEnable == 0)
    {
        i = BSP_LED_E_PRIORITY_HIGH;
        while (1)
        {
            if (tp_interface->t_Private.uca_PriorityMode[i] == (uint8_t)BSP_LED_E_MODE_NULL)
            {
                if (i == 0)
                {
                    break;
                }
                else
                {
                    i--;
                    continue;
                }
            }
            switch (tp_interface->t_Private.uca_PriorityMode[i])
            {
            case BSP_LED_E_MODE_NULL:
                break;
            case BSP_LED_E_MODE_ONCE:
                tp_interface->t_Private.uc_CycleTimer_100ms++;
                if (tp_interface->t_Private.uc_CycleTimer_100ms < 10)
                {
                    tp_interface->t_Output.fnp_SetPwm(tp_interface, BSP_LED_E_PWM_ALL_ON);
                }
                else
                {
                    tp_interface->t_Output.fnp_SetPwm(tp_interface, BSP_LED_E_PWM_ALL_OFF);
                    tp_interface->t_Private.uca_PriorityMode[i] = BSP_LED_E_MODE_NULL;
                    tp_interface->t_Private.uc_CycleTimer_100ms = 0;
                }
                break;
            case BSP_LED_E_MODE_TOGGLE:
                if (tp_interface->t_Private.uc_ReadOnOff == true)
                {
                    tp_interface->t_Output.fnp_SetPwm(tp_interface, BSP_LED_E_PWM_ALL_OFF);
                }
                else
                {
                    tp_interface->t_Output.fnp_SetPwm(tp_interface, BSP_LED_E_PWM_ALL_ON);
                }
                tp_interface->t_Private.uca_PriorityMode[i] = BSP_LED_E_MODE_NULL;
                break;
            case BSP_LED_E_MODE_FLICK:
                tp_interface->t_Private.uc_CycleTimer_100ms++;
                if (tp_interface->t_Private.uc_CycleTimer_100ms == 1)
                {
                    tp_interface->t_Output.fnp_SetPwm(tp_interface, BSP_LED_E_PWM_ALL_ON);
                }
                else if (tp_interface->t_Private.uc_CycleTimer_100ms == 2)
                {
                    tp_interface->t_Output.fnp_SetPwm(tp_interface, BSP_LED_E_PWM_ALL_OFF);
                }
                else if (tp_interface->t_Private.uc_CycleTimer_100ms >= 10)
                {
                    tp_interface->t_Private.uc_CycleTimer_100ms = 0;
                }
                break;
            case BSP_LED_E_MODE_PWM:
                tp_interface->t_Private.uc_CycleTimer_100ms++;
                if (tp_interface->t_Private.uc_CycleTimer_100ms <= 10)
                {
                    tp_interface->t_Output.fnp_SetPwm(tp_interface, (BSP_LED_E_PWM_LEVEL)tp_interface->t_Private.uc_CycleTimer_100ms);
                }
                else if (20 < tp_interface->t_Private.uc_CycleTimer_100ms && tp_interface->t_Private.uc_CycleTimer_100ms <= 30)
                {
                    tp_interface->t_Output.fnp_SetPwm(tp_interface, (BSP_LED_E_PWM_LEVEL)(30 - tp_interface->t_Private.uc_CycleTimer_100ms));
                }
                else if (tp_interface->t_Private.uc_CycleTimer_100ms >= 80)
                {
                    tp_interface->t_Private.uc_CycleTimer_100ms = 0;
                }
                break;
            case BSP_LED_E_MODE_ON:
                tp_interface->t_Output.fnp_SetPwm(tp_interface, BSP_LED_E_PWM_ALL_ON);
                break;
            case BSP_LED_E_MODE_OFF:
                tp_interface->t_Output.fnp_SetPwm(tp_interface, BSP_LED_E_PWM_ALL_OFF);
                break;
            default:
                tp_interface->t_Private.uc_CycleTimer_100ms = 0;
                break;
            }
            break;
        }
    }
    // 调试状态
    else
    {
        tp_interface->t_Private.uc_CycleTimer_100ms++;
        if (tp_interface->t_Private.uc_CycleTimer_100ms < 5)
        {
            tp_interface->t_Input.fnp_LedOnOff(true);
        }
        else if (tp_interface->t_Private.uc_CycleTimer_100ms < 10)
        {
            tp_interface->t_Input.fnp_LedOnOff(false);
        }
        else
        {
            tp_interface->t_Private.uc_CycleTimer_100ms = 0;
        }
    }
}

/**
 * @brief   控制PWM-LED
 * @note    改写pwm值
 * @param   pwmValue    -   0-10,其他-取反
 * @return  None
 */
static void BspLed_PwmLed(void *_tp_interface, BSP_LED_E_PWM_LEVEL _e_pwmLevel)
{
    BSP_LED_T_INTERFACE *tp_interface = _tp_interface;
    tp_interface->t_Private.e_PwmLevel = _e_pwmLevel;
    if (_e_pwmLevel == BSP_LED_E_PWM_ALL_OFF)
    {
        tp_interface->t_Private.uc_ReadOnOff = false;
    }
    else
    {
        tp_interface->t_Private.uc_ReadOnOff = true;
    }
}
/**
 * @brief   控制LED模式
 * @note    模式优先级大于单控
 * @param   mode              -   模式
            _e_Priority       -   控制等级
 * @return  None
 */
static void BspLed_Mode(void *_tp_interface, BSP_LED_E_MODE _e_mode, BSP_LED_E_PRIORITY _e_priority)
{
    BSP_LED_T_INTERFACE *tp_interface = _tp_interface;
    if (tp_interface->t_Private.uca_PriorityMode[_e_priority] == _e_mode)
    {
        return;
    }
    tp_interface->t_Private.uca_PriorityMode[_e_priority] = _e_mode;
    tp_interface->t_Private.uc_CycleTimer_100ms = 0;
}
/* 公有函数实体 (Public functions)---------------------------------------------*/
/**
 * @brief   LED初始化函数
 * @note    初始化GPIO
 * @param   None
 * @return  None
 */
void BspLed_Init(BSP_LED_T_INTERFACE *_tp_interface)
{
    _tp_interface->t_Input.fnp_LedOnOff(false);
    _tp_interface->t_Transplant.fnp_Cycle_1ms = BspLed_Cycle_1ms;
    _tp_interface->t_Transplant.fnp_Cycle_100ms = BspLed_Cycle_100ms;
    _tp_interface->t_Output.fnp_SetPwm = BspLed_PwmLed;
    _tp_interface->t_Output.fnp_SetMode = BspLed_Mode;
}
/**
 * @brief   Debug接口函数
 * @note    开启Debug相关指令后会循环点亮，关闭Debug后所有LED全部关闭
 * @param   _uc_trueOrFalse-判断指令进入与退出
 * @return  None
 */
void BspLed_DebugTestOnOff(uint8_t _uc_trueOrFalse, void *_vp_context)
{
    BSP_LED_T_INTERFACE *tp_interface = _vp_context;
    if (tp_interface == NULL)
    {
        return;
    }
    if (_uc_trueOrFalse == true)
    {
        tp_interface->t_Private.uc_DebugTestEnable = 1;
    }
    else
    {
        tp_interface->t_Private.uc_DebugTestEnable = 0;
        tp_interface->t_Input.fnp_LedOnOff(false);
    }
}

/********************************END OF FILE***********************************/
